package com.jh.user.pojo.payment;

import com.google.common.collect.ImmutableList;
import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * @author Fuchun
 * @since 1.0
 */
public class PaymentProfits {

    public static BigDecimal sumProfitAndFinished(@NotNull Collection<PaymentProfit> profits) {
        switch (profits.size()) {
            case 0: return BigDecimal.ZERO;
            case 1:
                PaymentProfit pp = profits.iterator().next();
                pp.setStatus(ProfitStatus.FINISHED);
                return pp.getTotalProfit();
            default:
                BigDecimal total = BigDecimal.ZERO;
                for (PaymentProfit profit : profits) {
                    total = total.add(profit.getTotalProfit());
                    profit.setStatus(ProfitStatus.FINISHED);
                }
                return total;
        }
    }

    public static PaymentProfit fromDto(@NotNull PosDTO dto) {
        PaymentProfit pp = new PaymentProfit();
        pp.setPosId(dto.getPosId());
        pp.setUserId(dto.getUserId());
        pp.setOrderType(dto.getOrderType());
        pp.setDealNumber(dto.getDealNumber());
        pp.setTotalAmount(dto.getTotalAmount());
        pp.setTotalProfit(dto.getTotalProfit());
        pp.setWeekOfYear(dto.getWeekOfYear());
        pp.setCreateTime(dto.getCreateTime());
        pp.setStatus(ProfitStatus.INITIAL);
        return pp;
    }

    @NotNull
    public static List<PaymentProfit> fromDto(List<PosDTO> dtoList) {
        if (dtoList == null || dtoList.isEmpty()) {
            return ImmutableList.of();
        }
        List<PaymentProfit> profits = new ArrayList<>(dtoList.size());
        for (PosDTO dto : dtoList) {
            profits.add(fromDto(dto));
        }
        return profits;
    }
}
